/**
 * Created by 000188 on 2016/6/29.
 */
module.exports = {
    createView(){
        if(!this.view){
            this.view = document.createElement('div');
            this.viewContent = document.createElement('div');
            this.view.appendChild(this.viewContent);
        }
        document.body.appendChild(this.view);
    },
    setStyle(){
        (function (v) {
            v.width = v.height =  '100%';
            v.background = 'rgba(255,255,255,.1)';
            v.position = 'fixed';
            v.top = 0;
            v.left = 0;
            v.lineHeight = '1rem';
            v.color = '#fff';
            v.textAlign = 'center';
            v.fontSize = '1.5rem';
        })(this.view.style);
        (function (v) {
            v.width =  '60%';
            v.background = 'rgba(0,0,0,.7)';
            v.position = 'fixed';
            v.top = '35%';
            v.left = '20%';
            v.lineHeight = '3rem';
            v.color = '#fff';
            v.textAlign = 'center';
            v.borderRadius = '.5rem';
            v.padding = '.5rem 1rem';
        })(this.viewContent.style)
    },
    alert(text, time){
        this.createView();
        this.setStyle();
        this.viewContent.innerHTML = text;
        if(!time){
            this.view.onclick = this.removeView.bind(this);
        }else if(time == -1){
            this.view.onclick = new Function;
            time = 3000;
        }
        this.time = setTimeout(()=> {
            this.removeView();
        }, (()=>{
            return time || 3000
        })())
    },
    removeView(){
        clearTimeout(this.time);
        document.body.removeChild(this.view);
    }
};